package download;

import bean.ThreadAllocationBean;
import util.Md5Utils;

import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;

/**
 * @Description  实现监听
 * @Author 冷彭磊
 * @Data 2025/2/26 10:14
 */
public class ConsoleDownloadListener implements DownloadListener{
    //下载链接
    private String filePath;
    //保存位置
    private String saveFilePath;
    //md5值
    private String md5;

    //上一次的进度
    private volatile int lastProgress = -1;


    @Override
    public void onStart(String filePath,String saveFilePath,String md5) {
        this.filePath = filePath;
        this.saveFilePath = saveFilePath;
        this.md5 = md5;
        System.out.println(saveFilePath + "开始下载");
    }

    @Override
    public void onComplete() {
        System.out.println(saveFilePath + "下载完成," + "保存位置" + saveFilePath);
    }

    //nowProgressFile文件的进度文件存储位置
    @Override
    public void onProgress(String nowProgressFile, long download, long total) {
        if (total == 0){
            onError("文件大小为0");
        }else{
            int percentage = (int) ((download * 100) / total);
            if (percentage > 100) {
                percentage = 100;
            }
            if (percentage - lastProgress >= 1){
                lastProgress = percentage;
                String formattedPercentage =percentage + "%";
                System.out.println(  saveFilePath +"线程" + Thread.currentThread().getId() + "下载进度：" + formattedPercentage);
            }

            //下载进度到达100%
            if (lastProgress == 100){
                //删除进度文件
                new File(nowProgressFile).delete();
                onComplete();
                onMD5VerifySuccessful();
            }

        }

    }

    @Override
    public void onError(String message) {
        System.out.println( filePath + "下载出错:" + message);

    }

    @Override
    public void onFileExits(String fileName ) {
        System.out.println(fileName + "文件已存在,无需重复下载");

    }

    @Override
    public void onBreakpointContinuation(String filePath,boolean support) {
        if (support){
            System.out.println(filePath + "支持断点续传");
        }else{
            System.out.println(filePath + "不支持断点续传");

        }
    }

    @Override
    public void onMD5VerifySuccessful( ) {

        final File file = new File(saveFilePath);
        if (file.exists()){
            try {
                final String localMD5 = Md5Utils.calculateMd5(file);
                if (localMD5.equals(md5)){
                    System.out.println(saveFilePath + "MD5校验成功");
                }else{
                    System.out.println(saveFilePath + "MD5校验失败");
                    System.out.println("原始MD5:" + md5);
                    System.out.println("本地MD5:" + localMD5);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        }else{
            System.out.println(saveFilePath + "文件不存在无法校验");
        }

    }

    @Override
    public void onThreadAllocation(ThreadAllocationBean threadAllocationBean) {
        System.out.println(threadAllocationBean.getLocalFile() + "线程" + threadAllocationBean.getThreadId()+ ":" + threadAllocationBean.getStart() + "-" + threadAllocationBean.getEnd()) ;
    }


}




